package com.liduanwei.update.util;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

/**
 * Created by Wei on 17/10/3.
 */
public class HttpUtil {
    public static void download(String url, DownloadCallback downloadCallback) throws IOException {
        HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection();
        InputStream in = connection.getInputStream();
        byte[] buffer = new byte[1024 << 1];
        int len;
        while ((-1 != (len = in.read(buffer)))) {
            downloadCallback.onProgress(buffer, len);
        }
        downloadCallback.onComplete(true);
        in.close();
    }

    public interface DownloadCallback {
        void onProgress(byte[] bytes, int len);

        void onComplete(boolean isSuccessfully);
    }
}
